package com.adams.dcxt.facade;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adams.dcxt.bean.Order;
import com.adams.dcxt.bean.OrderDishes;
import com.adams.dcxt.core.Contants;
import com.adams.dcxt.core.Page;
import com.adams.dcxt.service.OrderDishesService;
import com.adams.dcxt.service.OrderService;

@Service("orderFacade")
@Transactional
public class OrderFacadeImpl implements OrderFacade {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDishesService orderDishesService;

    @Override
    public Page<OrderDishes> getOrderDishes(String tableId, Page<OrderDishes> page) {

        Order order = orderService.queryOrderByTable(tableId);
        if (order != null) {
            page = orderDishesService.getListByOrderId(order.getId(), page);
        }
        return page;
    }

    @Override
    public void placeOrder(Order o, List<OrderDishes> odishesList ,Order oldOrder) {
    	if(oldOrder==null){
    		orderService.saveOrder(o);
    		oldOrder = o;
    	}
        for(OrderDishes  os : odishesList){
            os.setOrderId(oldOrder.getId());
            os.setPresentFlag(Contants.NO_PRESENT);
            os.setReadyState(Contants.READY_STATE_ING);
            orderDishesService.save(os);
        }
    }

}
